<template>
	<view>
		<u-modal :show="show" :title="title" :content='content' @confirm="confirm()"></u-modal>
		<u-toast ref="uToast"></u-toast>
	</view>
</template>

<script>
	import {userLogin} from '@/api/user.js'
	export default {
		name:"login",
		data() {
			  return {
				msgType: 'success',
				show:this.$store.state.loginShow,
				title:'温馨提示',
				content:'只有登录了微信才能使用该小程序购买商品'
			  }
		},
		created(){
			
			if(this.$store.getters.token == ''){
				console.log('show:')
				console.log(this.$store.state.loginShow)
				this.show = true
			}
			
		},
		watch:{
			'$store.state.loginShow':function(newVal){
				console.log(newVal)
				this.show = newVal
			}
		},
		methods: {
			confirm(){
				console.log('confirm')
				wx.login({
					success: (res) => {
						console.log(res)
						const config = {
							code : res.code
						}
						userLogin(config).then(res=>{
							console.log(res.data)
							//存储个人信息
							this.$store.dispatch('login',res.data)
							this.show = false
							this.$refs.uToast.show({
								message:'登录成功',
								type: 'success',
							})
							// uni.navigateTo({
							// 	url:'/pages/home/home'
							// })
						}).catch(err=>{
							this.$refs.uToast.show({
								message:'登录失败'+err,
								type: 'error',
							})
						})
						
						
					},
				})

			},
			  open() {
				// console.log('open');
			  },
			  close() {
				this.show = false
				// console.log('close');
			  }
		}
	}
</script>

<style>

</style>